#!/usr/bin/python3
# -*- coding: utf-8 -*-


from utils import *

__all__ = [
    "insurance_numbers_strategy",
    "insurance_peoples_strategy",
    "insurance_dates_strategy",
    "insurance_expands_strategy",
    "license_plate_numbers_strategy",
    "engine_numbers_strategy",
    "frame_numbers_strategy"
]


def insurance_numbers_strategy(choices, min_length=8):
    """
    保单号的选取规则和策略

    :param min_length:
    :param choices:
    :return:
    """

    if not choices:
        return ""
    number = ""
    number += matching_alpha_digit_special(choices[0])
    if len(number) > min_length:
        return number

    for char in "".join(choices[1:]):
        if matching_alpha_digit_special(char):
            number += char
        if matching_chinese(char):
            break
    return number


def insurance_peoples_strategy(choices, importance=0, order=-1):
    """
    被保险人名称的选取规则和策略

    :param choices:
    :param importance:
    :return:

    Parameters
    ----------
    order
    """

    if not choices:
        return ""

    # for choice in choices[importance + 1:] + choices[:importance][::-1] + [choices[importance]]:
    #     if any(["公司" in choice, "有限" in choice, "有正式名称" in choice]):
    #         if any(["名称" in choice, "正式名称" in choice]):
    #             choice = choice.replace("正式名称", "")
    #             choice = choice.replace("名称", "")
    #         if choice[-1] == "有":
    #             choice += "限责任"
    #         if "公司" not in choice:
    #             choice += "公司"
    #         choice = choice[:choice.index("公司") + 2]
    #         return choice

    # 判断是否是公司
    name = ""
    for i, choice in enumerate(choices[importance:] + choices[:importance][::order]):
        if any([i == 0, i == 1]) and "公司" in choice:
            choice = choice[:choice.index("公司") + 2]
            return choice
        if len(choice) > 30:
            continue

        if any(["名称" in choice, "正式名称" in choice]):
            choice = choice.replace("正式名称", "")
            choice = choice.replace("名称", "")
            name += choice

        if "公司" in choice:
            name += matching_chinese(choice[:choice.index("公司") + 2])
        if "司" in choice and "公司" not in choice:
            name += matching_chinese(choice[:choice.index("司") + 1])
        if name and name[-1] == "司" and len(name) > 6:
            return name

    # 判断是否是人的名字
    for i, choice in enumerate(choices[importance + 1:] + choices[:importance][::-1] + [choices[importance]]):
        if is_people_name(choice):
            return choice
    return ""

    # name = choices[importance + 1]
    # if matching_digit(name):
    #     return ""
    # return name


def insurance_dates_strategy(choices):
    """
    保险期间选取规则和策略

    :param choices:
    :return:
    """

    if not choices:
        return "", ""

    choices_str = "".join(choices)

    choices_str = choices_str.replace("年", "-")
    choices_str = choices_str.replace("年", "")
    choices_str = choices_str.replace("⽉", "")
    choices_str = choices_str.replace("月", "-")

    choices = re.findall(r"(\d{4}-\d{1,2}-\d{1,2})", choices_str)
    if len(choices) >= 2:
        return choices[:2]
    elif not choices:
        return "", ""
    else:
        return choices[0], ""


def insurance_expands_strategy(choices):
    """
    保险费合计选取规则和策略

    :param choices:
    :return:
    """
    if not choices:
        return ""

    choices = "".join(choices)
    chinese_digit = ["零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖", "圆", "万", "亿", "兆", "拾", "佰", "仟", "角", "分"]
    choices_str = " "
    for char in choices:
        if char in chinese_digit and char != choices_str[-1]:
            if char == "分" and choices_str[-1] == "角":
                continue
            choices_str += char
    choices_str = choices_str[1:]

    return choices_str


def license_plate_numbers_strategy(choices, importance=0):
    """
    号码号牌的选取规则和策略

    :param choices:
    :param importance:
    :return:
    """

    if not choices:
        return ""
    province = list("京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁台琼使领军北南成广沈济空海")
    search_pattern = r'[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁台琼使领军北南成广沈济空海]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂领学警港澳]{1}(?!\d)'

    for choice in choices[importance + 1:] + choices[:importance][::-1] + [choices[importance]]:
        if not choice:
            continue
        number = re.findall(search_pattern, choice.replace("-", ""))
        if number:
            return number[0]

        number = ""
        for i, char in enumerate(choice):
            if char in province:
                number += choice[i]
                for j, c in enumerate(choice[i + 1:]):
                    # 特殊案例
                    if j == 0 and c in "新":
                        number += c
                        continue
                    if matching_alpha_digit_special(c):
                        number += c
                    else:
                        if c in "挂领学警港澳":
                            number += c
                        break
                break
        if len(number) > 4:
            return number

        # if choice[0] in province and matching_alpha_digit(choice) and len(_matching_chinese(choice)) <= 2:
        #     return choice
        # choice_ = ""
        # for char in choice_[1:]:
        #     if matching_alpha_digit(char):
        #         choice_ += char
        #     else:
        #         break
        # return choice_

    number = choices[importance + 1]

    number = number.replace("⻋", "")
    number = number.replace("号牌号码", "")

    number_cn = matching_chinese(number)
    number_en_d = matching_alpha_digit(number)
    if not number_en_d:
        if "新车" in number_cn:
            return "新车"
        else:
            return ""
    if len(number_cn) == 1:
        return number_cn + number.replace(number_cn, "")
    elif len(number_cn) == 2:
        number = number.replace(number_cn[0], "")
        number = number.replace(number_cn[1], "")
        return number_cn[0] + number + number_cn[1]
    else:
        return ""


def engine_numbers_strategy(choices, importance=0):
    """
    发动机号的选取规则和策略

    :param choices:
    :param importance:
    :return:
    """

    if not choices:
        return ""

    for choice in choices[importance + 1:] + choices[:importance][::-1] + [choices[importance]]:
        return matching_alpha_digit(choice)
    return ""


def frame_numbers_strategy(choices, importance=0):
    """
    车架号的选取规则和策略

    :param choices:
    :param importance:
    :return:
    """

    if not choices:
        return ""

    for choice in choices[importance + 1:] + choices[:importance][::-1] + [choices[importance]]:
        number = matching_alpha_digit_special(choice)
        if len(number) > 13:
            return number
    # number = matching_alpha_digit(choices[importance + 1])
    return ""
